//
//  AppDelegate.m
//  Guardian
//
//  Created by 代佳宏 on 2022/4/14.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[BizApplication shareInstance] configWithOptions:launchOptions];
    [[BizApplication shareInstance] configRootWindowWithOptions:launchOptions];

    return YES;
}


#pragma mark - 快速获取当前界面
- (UIViewController *)getCurrentVC {
    UIViewController *result = nil;
//    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    UIWindow * window = [self keyWindow];

    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
      
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    return result;
}

-(UIWindow*)keyWindow{
    UIWindow *foundWindow = nil;
    NSArray  *windows = [[UIApplication sharedApplication]windows];
    for (UIWindow  *window in windows) {
        if (window.isKeyWindow) {
            foundWindow = window;
            break;
        }
    }
    return foundWindow;

}

- (UIViewController *)getCurrentUIVC {
    UIViewController  *superVC = [self getCurrentVC];
    
    if ([superVC isKindOfClass:[UITabBarController class]]) {
        UIViewController  *tabSelectVC = ((UITabBarController *)superVC).selectedViewController;
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }
    else if ([superVC isKindOfClass:[UINavigationController class]]) {
        return ((UINavigationController *)superVC).viewControllers.lastObject;
    }
    
    return superVC;
}
@end
